//
//  songleiViewController.m
//  GSG
//
//  Created by gaox on 15/4/24.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "songleiViewController.h"
#import "AFNetworking.h"
#import "MusicModel.h"
#import "UIImageView+WebCache.h"
#import "SongBookCell.h"
#import "SongModel.h"
#import "PlayerViewController.h"
#import "MBProgressHUD.h"
#import "singleSong.h"
#import"PlayListViewController.h"
@interface songleiViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSMutableArray *modelArr;
@property (nonatomic,retain)UITableView *songeleTableView;
@property(nonatomic,retain)MBProgressHUD*HUD;
@property(nonatomic,copy)NSString *str1;
@property(nonatomic,copy)NSString *string;
@property(nonatomic,retain)NSMutableArray *Arr;
@end
 
@implementation songleiViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.modelArr = [NSMutableArray array];
        self.Arr = [NSMutableArray array];
    }
    
    return self;
}

- (void)data{


//    NSLog(@"----------%ld",self.musicModel.msg_id);


  //    NSString *str = [NSString stringWithFormat:@"http://v1.ard.q.itlily.com/share/user_timeline?msg_ids=%5B%ld%5D",self.musicModel._id];

    NSString *str = @"http://v1.ard.q.itlily.com/share/user_timeline?msg_ids=%5B%ld%5D";
    NSString *a = [NSString stringWithFormat:@"%ld",self.musicModel.msg_id];
    NSString *strNew = [str stringByReplacingOccurrencesOfString:@"%ld" withString:a];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:strNew parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dict = [NSJSONSerialization  JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSMutableArray *arr = [dict objectForKey:@"data"];
        
        NSDictionary *dic=arr[0];
        NSMutableArray *arr1=[NSMutableArray array];
          arr1=[dic objectForKey:@"songlist"];

        for (int i=0;i<arr1.count;i++){
            self.str1=[NSString stringWithFormat:@",%@",[arr1[i] objectForKey:@"song_id"]];
        [self.Arr addObject:self.str1];
        }




       
        self.string = nil;
        NSString *lastString = @"";
        for (NSString *value in self.Arr) {
            self.string = [NSString stringWithFormat:@"%@%@", lastString, value];

            lastString = [NSString stringWithFormat:@"%@", self.string];
        }
            [self dataload];
        [self.songeleTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"No");
        NSLog(@"%@",error);
    }];

}
- (void)dataload{
    NSString *str = [NSString stringWithFormat:@"http://ting.hotchanson.com/songs/downwhite?song_id=%@",self.string];
//    NSLog(@"------%@",str);
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dict = [NSJSONSerialization  JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
      
        
        NSMutableArray *arr = [dict objectForKey:@"data"];
        
             for (NSInteger i = 0; i < arr.count ; i++) {
            SongModel *mo = [[SongModel alloc]init];
            NSMutableDictionary *dic1 = arr[i];
            mo.song_name=[dic1 objectForKey:@"song_name"];
            mo.singer_name=[dic1 objectForKey:@"singer_name"];
            NSMutableArray *arr = [dic1 objectForKey:@"url_list"];
            
            for (NSDictionary *dic  in arr) {
                mo.url=[dic objectForKey:@"url"];
                
            }
                

                 [self.modelArr addObject:mo];
            [mo release];



        }

        
        
        [self.songeleTableView reloadData];
        [_HUD hide:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"No");
        NSLog(@"%@",error);
    }];

    
    
    
}

-(void)actionBuLand{
    PlayListViewController *plvc=[[PlayListViewController alloc]init];
    [self.navigationController pushViewController:plvc animated:YES];
    [plvc release];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"播放" style:UIBarButtonItemStyleDone target:self action:@selector(actionBuLand)];
    self.navigationItem.rightBarButtonItem.tintColor=[UIColor whiteColor];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bujing.png"]];
    
    [self data];
    self.title = self.musicModel.title;

    self.songeleTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-self.view.frame.size.height * 50 / 667) style:UITableViewStylePlain];
    self.songeleTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.songeleTableView];
    self.songeleTableView.rowHeight = 100;
    self.songeleTableView.dataSource = self;
    self.songeleTableView.delegate = self;
    //小菊花
    //方式1.直接在View上show
    _HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    
    
    //常用的设置
    //小矩形的背景色
    _HUD.color = [UIColor clearColor];//这儿表示无背景
    //显示的文字
    _HUD.labelText = @"提示";
    //细节文字
    _HUD.detailsLabelText = @"正在加载内容";
    //是否有庶罩
    _HUD.dimBackground = YES;


}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return  self.modelArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse = @"reuse";
     SongBookCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[SongBookCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
    }
    
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    SongModel *song=self.modelArr[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"%ld.%@",indexPath.row +1,song.song_name];

    cell.song_nameLabel.text = str;
    cell.singer_nameLabel.text = song.singer_name;

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    PlayerViewController *pvc=[[PlayerViewController alloc]init];
//    
//    
//    pvc.arr=self.modelArr;
//    pvc.num=indexPath.row;
//    [self.navigationController pushViewController:pvc animated:YES];
//    
//    [pvc release];
    [singleSong shareSingleSong].arrSong=self.modelArr;
    [singleSong shareSingleSong].num=indexPath.row;
    
    NSNotificationCenter *center=[NSNotificationCenter  defaultCenter];
    
    [center postNotificationName:@"播放" object:@"收到了看见了" userInfo:nil];
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
